import { selectCategorizeInfoDict } from "@/api/factor/categorizeInfo";
import { handleTree } from "@/utils/baseUtil";
import { selectProductCategorizeDict} from "@/api/factor/productCategorize";
import { selectEnterpriseInfoDict} from "@/api/factor/enterpriseInfo";
const factorDict = {
  state: {
    //分类字典
    categorizeInfoOptions:[],
    categorizeTreeDict:[],//包含停用分类
    categorizeTreeDict1:[],//不包含停用分类
    //分类类型 1工业产品  2能源产品 3生活产品 4废弃物处理 5交通服务 6碳汇
    typeOptions:[
      {dictValue: 1, dictLabel: "工业产品"},
      {dictValue: 2, dictLabel: "能源产品"},
      {dictValue: 3, dictLabel: "生活产品"},
      {dictValue: 4, dictLabel: "废弃物处理"},
      {dictValue: 5, dictLabel: "交通服务"},
      {dictValue: 6, dictLabel: "碳汇"},
    ],
    //分类级别 1级2级3级
    levelOptions:[
      {dictValue: 1, dictLabel: "1级分类"},
      {dictValue: 2, dictLabel: "2级分类"},
      {dictValue: 3, dictLabel: "3级分类"},
    ],
    //状态 （1正常 2停用）
    statusOptions:[
      {dictValue: 1, dictLabel: "正常"},
      {dictValue: 2, dictLabel: "停用"},
    ],
    //标准阶段环节 1阶段 2环节
    standardStageTypeOptions:[
      {dictValue: 1, dictLabel: "阶段"},
      {dictValue: 2, dictLabel: "环节"},
    ],
    //1上游排放  2下游排放
    factorTypeOptions:[
      {dictValue: 1, dictLabel: "上游排放"},
      {dictValue: 2, dictLabel: "下游排放"},
    ],
    //产品分类 分类级别 0门类 1大类 2中类 3小类
    productCategorizeOptions:[
      {dictValue: 0, dictLabel: "门类"},
      {dictValue: 1, dictLabel: "大类"},
      {dictValue: 2, dictLabel: "中类"},
      {dictValue: 3, dictLabel: "小类"},
    ],
    // 0门类
    productCategorizeDict0:[],
    //1大类
    productCategorizeDict1:[],
    //2中类
    productCategorizeDict2:[],
    //3小类
    productCategorizeDict3:[],
    //企业列表
    enterpriseOptions:[],
    enterpriseDict:[],
    //企业类型  (1发电企业、2电网企业、3镁冶炼企业、4铝冶炼企业、5钢铁生产企业、6民用航空企业、 7平板玻璃生产企业、8水泥生产企业、9陶瓷生产企业、10化工生产企业、0其他企业)
    enterpriseTypeOptions:[
      {dictValue: 1, dictLabel: "发电企业"},
      {dictValue: 2, dictLabel: "电网企业"},
      {dictValue: 3, dictLabel: "镁冶炼企业"},
      {dictValue: 4, dictLabel: "铝冶炼企业"},
      {dictValue: 5, dictLabel: "钢铁生产企业"},
      {dictValue: 6, dictLabel: "民用航空企业"},
      {dictValue: 7, dictLabel: "平板玻璃生产企业"},
      {dictValue: 8, dictLabel: "水泥生产企业"},
      {dictValue: 9, dictLabel: "陶瓷生产企业"},
      {dictValue: 10, dictLabel: "化工生产企业"},
      {dictValue: 0, dictLabel: "其他企业"},
    ],
    //企业状态     1正常 2停用
    enterpriseStatusOptions:[
      {dictValue: 1, dictLabel: "正常"},
      {dictValue: 2, dictLabel: "停用"},
    ],


  },
  mutations: {
    SET_CATEGORIZE_INFO_OPTIONS: (state, list) => {
      list.forEach(function(obj) {
        obj.idStr = obj.id+"";
        if(obj.englishName){
          obj.name =obj.chineseName+'【'+obj.englishName+'】'
        }else {
          obj.name =obj.chineseName
        }
      });
      state.categorizeInfoOptions = list;
      state.categorizeTreeDict = handleTree(list, "id", "parentId");
      state.categorizeTreeDict1 = handleTree(list.filter(item => item.status===1), "id", "parentId");
    },
    SET_PRODUCT_CATEGORIZE_DICT: (state, list) => {
      let p0 = [],p1=[],p2=[],p3=[];
      for (let item of list){
        item['code_key']=item['level']+"_"+item['code'];
        if(item.level==0){
          p0.push(item);
        }else if(item.level==1){
          p1.push(item);
        }else if(item.level==2){
          p2.push(item);
        }else if(item.level==3){
          p3.push(item);
        }
      }
      for (let father of p2) {
        father['children'] = p3.filter(child => { return father['code'] === child['parentCode']});
      }
      for (let father of p1) {
        father['children'] = p2.filter(child => { return father['code'] === child['parentCode']});
      }
      for (let father of p0) {
        father['children'] = p1.filter(child => { return father['code'] === child['parentCode']});
      }
      state.productCategorizeDict0=p0;
      state.productCategorizeDict1=p1;
      state.productCategorizeDict2=p2;
      state.productCategorizeDict3=p3;
    },
    SET_ENTERPRISE_DICT: (state, list) => {
      state.enterpriseOptions = list;
      state.enterpriseDict = handleTree(list, "id", "parentId");
    },
  },
  actions: {
    SelectCategorizeDict({ commit }, userInfo) {
      selectCategorizeInfoDict().then(res=>{
        commit('SET_CATEGORIZE_INFO_OPTIONS', res.data);
      });
    },
    SelectProductCategorizeDict({ commit }, userInfo) {
      selectProductCategorizeDict().then(res=>{
        commit('SET_PRODUCT_CATEGORIZE_DICT', res.data);
      });
      selectEnterpriseInfoDict().then(res=>{
        commit('SET_ENTERPRISE_DICT', res.data);
      });
    },

  }
};
export default factorDict
